#include <types.h>
#include <nios2_timer.h>
#include <nios2.h>
#include <system.h>
#include <alt_types.h>
#include <altera_avalon_timer_regs.h>
#include <clock.h>
#include <stdio.h>
#include <nios2_irq.h>

static void
alt_avalon_timer_sc_init (void* base, alt_u32 irq_controller_id, alt_u32 irq, alt_u32 freq)
{
  /* set the system clock frequency */
  
  //alt_sysclk_init (freq);//TODO : clock frequency, in alt_alarm.h
  
  /* set to free running mode */
   
    IOWR_ALTERA_AVALON_TIMER_CONTROL (base, 
         ALTERA_AVALON_TIMER_CONTROL_ITO_MSK  |
         ALTERA_AVALON_TIMER_CONTROL_CONT_MSK |
         ALTERA_AVALON_TIMER_CONTROL_START_MSK);
         
    alt_irq_enable(irq);
}



/* timer_init() */
void
timer_init(void) {
    alt_avalon_timer_sc_init((void*) (TIMER_BASE + 0xc0000000),
                               0,
                               TIMER_IRQ,
                               1000 //TODO: freq of timer
    );
    cprintf("++ setup timer interrupts\n");
}



